    /**
     * 高德调用退款接口
     */
    public function refundOrder(){
        $data = request()->param();
        $checkToken = $this->checkGaodeSign('refundOrder', '高德调用退款接口');
        if ($checkToken !== true) {
            return $checkToken;
        }

        $data['bizReq'] = is_array($data['bizReq']) ? $data['bizReq'] : json_decode($data['bizReq'], true);

        if (!isset($data['bizReq']['payRequestId']) || !$data['bizReq']['payRequestId']) {
            return $this->returnGaoDeMapResult('API00000001', '参数异常，payRequestId不能为空');
        }
        if (!isset($data['bizReq']['refundRequestId']) || !$data['bizReq']['refundRequestId']) {
            return $this->returnGaoDeMapResult('API00000001', '参数异常，refundRequestId不能为空');
        }

        $returnBizRes = [
            'refundRequestId' => $data['bizReq']['refundRequestId'],
            'refundAmount' => $data['bizReq']['refundAmount'],
        ];

        try {
            $orderMod = new Orders();
            $ordersRefundModel = new OrdersRefund();

            $orderDetail = $orderMod->where(['pay_request_id' => $data['bizReq']['payRequestId'], 'pay_status' => 2])->find();//dd($data);
            if (!$orderDetail) {
                return $this->returnGaoDeMapResult('API00000012', '无可退记录');
            }
            $orderDetail = $orderDetail->toArray();

            //根据高德订单号查找更新或者新增
            $logModel = new InterfaceLog();
            $logData = [
                'o_id'           => $orderDetail['amap_order_id'],
                'type'           => 2,
                'interface_name' => '高德退款接口',
                'action_url'     => '/api/coupons/Gaodemap/refundOrder',
                'content'        => json_encode($data, JSON_UNESCAPED_UNICODE),
                'create_time'    => date('Y-m-d H:i:s')
            ];
            $LogId = 0;
            if ($logModel->save($logData)) {
                $LogId = $logModel->getLastInsID();
            }

            if ($ordersRefundModel->where(['oid' => $orderDetail['id'], 'status' => 0])->find()) {
                $logModel->where(['id' => $LogId])->update(['result' => '高德调用退款失败，有一笔退款正在进行中']);
                return $this->returnGaoDeMapResult('API00000006', '有一笔退款正在进行中，请稍后再请求');
            }


            if (($orderDetail['total_fee'] - $orderDetail['refund_amount']) < $data['bizReq']['refundAmount']) {
                return $this->returnGaoDeMapResult('API00000007', '超出可退金额');
            }

            $refundDetail = $ordersRefundModel->where(['refund_request_id' => $data['bizReq']['refundRequestId']])->find();
            if ($refundDetail && $refundDetail['status'] == 1) {
                $returnBizRes['refundTradeNo'] = $refundDetail['refund_trade_no'];
                $returnBizRes['tradeStatus'] = 'SUCCESS';
                $returnBizRes['tradeFinishTime'] = $refundDetail['finish_time'];

                if ($LogId) {
                    $logModel->where(['id' => $LogId])->update(['result' => '高德调用退款成功', 'status' => 1]);
                }
                $this->notifyGaodeOrderStatus($orderDetail['amap_order_id'], $refundDetail);
                return $this->returnGaoDeMapResult('API00000000', '成功', 'true', $returnBizRes);
            }

            $refundTradeNo = str_replace('BYDDD', 'BYDTK', $orderDetail['order_sn']) . substr($data['bizReq']['refundRequestId'], -3);
            $refundData = [
                'oid'               => $orderDetail['id'],
                'amount'            => $data['bizReq']['refundAmount'],
                'refund_reason'     => isset($data['bizReq']['refundReason']) ? $data['bizReq']['refundReason'] : '',
                'notify_url'        => $data['bizReq']['notifyUrl'],
                'refund_request_id' => $data['bizReq']['refundRequestId'],
                'refund_trade_no'   => $refundTradeNo,
                'request_time'      => $data['bizReq']['requestTime'],
            ];

            //计算本次退款钱、券各退多少
            if ($orderDetail['person_amount'] && ($orderDetail['person_amount'] - $orderDetail['refund_amount']) > 0) {
                //存在个人支付时，优先退个人支付部分
                $refundPersonAmount = $orderDetail['person_amount'] - $orderDetail['refund_amount'] - $data['bizReq']['refundAmount'];
                if ($refundPersonAmount >= 0) {
                    //个人支付金额足够抵扣本次退款，则不退券
                    $refundData['enterprise_amount'] = 0;
                    $refundData['person_amount'] = $data['bizReq']['refundAmount'];
                } else {
                    //个人支付金额不足以抵扣本次退款
                    $refundData['enterprise_amount'] = $data['bizReq']['refundAmount'] - ($orderDetail['person_amount'] - $orderDetail['refund_amount']);
                    $refundData['person_amount'] = $orderDetail['person_amount'] - $orderDetail['refund_amount'];
                }
            } else {
                //退券
                $refundData['person_amount'] = 0;
                $refundData['enterprise_amount'] = $data['bizReq']['refundAmount'];
            }

            //记录用户退款记录
            $ordersRefundModel->insert($refundData, true);

            $refundData['status'] = 1;
            $payService = new Pay();
            //当只有个人退款时直接调用通联支付退款接口
            if ($refundData['person_amount'] && !$refundData['enterprise_amount']) {
                //调用通联支付退款方法
                $payRefundRes = $payService->refundWxPayOrder($orderDetail, $refundData);
                if (!$payRefundRes) {
                    $returnBizRes['tradeStatus'] = 'PROCESSING';
                    $ordersRefundModel->where(['refund_request_id' => $data['bizReq']['refundRequestId']])->update(['status' => 2]);
                    if ($LogId) {
                        $logModel->where(['id' => $LogId])->update(['result' => '高德调用退款失败']);
                    }
                    $refundData['status'] = 2;
                    $this->notifyGaodeOrderStatus($orderDetail['amap_order_id'], $refundData);
                    return $this->returnGaoDeMapResult('API00000011', '退款处理中', 'true', $returnBizRes);
                }
            }

            //如果有券退款时，采用回滚机制依次退券和退钱
            if ($refundData['enterprise_amount']) {
                //调用退券接口方法
                $couponsRefundRes = $payService->refundCoupons($orderDetail, $refundData);
                if (!$couponsRefundRes) {
                    $returnBizRes['tradeStatus'] = 'PROCESSING';
                    $ordersRefundModel->where(['refund_request_id' => $data['bizReq']['refundRequestId']])->update(['status' => 2]);
                    if ($LogId) {
                        $logModel->where(['id' => $LogId])->update(['result' => '高德调用退款失败']);
                    }
                    $refundData['status'] = 2;
                    $this->notifyGaodeOrderStatus($orderDetail['amap_order_id'], $refundData);
                    return $this->returnGaoDeMapResult('API00000011', '退款处理中', 'true', $returnBizRes);
                }
            }
            if ($LogId) {
                $logModel->where(['id' => $LogId])->update(['result' => '高德调用退款成功', 'status' => 1]);
            }
            $ordersRefundModel->where(['refund_request_id' => $data['bizReq']['refundRequestId']])->update(['status' => 1, 'finish_time' => date('Y-m-d H:i:s')]);
            $this->notifyGaodeOrderStatus($orderDetail['amap_order_id'], $refundData);
            
            $returnBizRes['refundTradeNo'] = $refundTradeNo;
            $returnBizRes['tradeStatus'] = 'SUCCESS';
            $returnBizRes['tradeFinishTime'] = date('Y-m-d H:i:s');
            return $this->returnGaoDeMapResult('API00000000', '成功', 'true', $returnBizRes);
        }catch (Exception $exception){
            $returnBizRes['tradeStatus'] = 'PROCESSING';
            (new WebhookService())->sendMsg('(refundOrder)异步通知高德订单状态发生异常错误，高德支付流水号订单号为：'. $data['bizReq']['payRequestId'] .'，错误信息为：' . $exception->getMessage());
            trace('(refundOrder)异步通知高德订单状态发生异常错误，高德支付流水号订单号为：'. $data['bizReq']['payRequestId'] .'，错误信息为：' . $exception->getMessage(), 'error');
            return $this->returnGaoDeMapResult('API00000011', '退款处理中', 'true', $returnBizRes);
        }
    }